package com.xfcycle.zebrabikegp.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.xfcycle.zebrabikegp.bean.SportDataResult;

/**
 * 运动数据Dao类
 * Created by Wilson on 2017/6/22.
 */

public class SportDataDao {

    private SportDataDBHelper sportDataDBHelper;

    public SportDataDao(Context context) {
        sportDataDBHelper = new SportDataDBHelper(context);
    }

    /**
     * 添加运动数据
     * 这里当前的具体时间无需写入
     *
     * @param sportDataResult 需要添加的运动数据类
     */
    public void addSportData(SportDataResult sportDataResult) {
        SQLiteDatabase db = sportDataDBHelper.getWritableDatabase();
        String addSql = "insert into " + SportDataDBHelper.TBL_SPORT_DATA_HISTORY + "(uuid,bike_id,distance,calorie,pace,power,time,date,target) values(?,?,?,?,?,?,datetime('now'),?,?)";
        db.execSQL(addSql, new Object[]{sportDataResult.getUuid(), sportDataResult.getBike_id(), sportDataResult.getDistance(),
                sportDataResult.getCalorie(), sportDataResult.getPace(), sportDataResult.getPower(), sportDataResult.getDate(), sportDataResult.getTarget()});
        db.close();

        Log.d("数据库Dao", "addSportData: -------------------------------------------------- 添加数据：" + sportDataResult.toString());
    }

    /**
     * 删除特定自行车运动数据
     *
     * @param bike_id 需要删除数据的自行车id
     */
    public void deleteSportData(String bike_id) {
        SQLiteDatabase db = sportDataDBHelper.getWritableDatabase();
        String deleteSql = "delete from " + SportDataDBHelper.TBL_SPORT_DATA_HISTORY + "where bike_id=?";
        db.execSQL(deleteSql, new Object[]{bike_id});
        db.close();
    }

    /**
     * 更新特定数据
     *
     * @param sportDataResult 需要修改的运动数据类
     */
    public void updateSportData(SportDataResult sportDataResult) {
        SQLiteDatabase db = sportDataDBHelper.getWritableDatabase();
        String updateSql = "update " + SportDataDBHelper.TBL_SPORT_DATA_HISTORY + "set distance=?,time=?,calorie=?,pace=?,power=?,date=?,target=? where bike_id=?";
        db.execSQL(updateSql, new Object[]{sportDataResult.getDistance(), sportDataResult.getTime(), sportDataResult.getCalorie(),
                sportDataResult.getPace(), sportDataResult.getPower(), sportDataResult.getDate(), sportDataResult.getTarget(), sportDataResult.getBike_id()});
        db.close();
    }

    /**
     * 查询特定自行车 特定时间 运动数据 显示到手机
     *
     * @param bike_id 需要查询的自行车id
     * @param date    需要查询的时间
     */
    public SportDataResult querySportData(String bike_id, String date) {
        SQLiteDatabase db = sportDataDBHelper.getReadableDatabase();

        String selectSql = "select count(time) as count,sum(distance) as distance,sum(calorie) as calorie,avg(pace) as pace,avg(power) as power,max(target) as target from "
                            + SportDataDBHelper.TBL_SPORT_DATA_HISTORY + " where bike_id =? and date=?";
        Cursor cursor = db.rawQuery(selectSql, new String[]{bike_id, date});

        SportDataResult sportDataResult = new SportDataResult();
        while (cursor.moveToNext()) {

            sportDataResult.setTime(-1 == cursor.getInt(cursor.getColumnIndex("count")) ? 0 : cursor.getInt(cursor.getColumnIndex("count"))*15);
            sportDataResult.setDistance(null == cursor.getString(cursor.getColumnIndex("distance")) ? "0" : cursor.getString(cursor.getColumnIndex("distance")));
            sportDataResult.setCalorie(null == cursor.getString(cursor.getColumnIndex("calorie")) ? "0" : cursor.getString(cursor.getColumnIndex("calorie")));
            sportDataResult.setPace(null == cursor.getString(cursor.getColumnIndex("pace")) ? "0" : cursor.getString(cursor.getColumnIndex("pace")));
            sportDataResult.setPower(null == cursor.getString(cursor.getColumnIndex("power")) ? "0" : cursor.getString(cursor.getColumnIndex("power")));
            sportDataResult.setTarget(Integer.parseInt(null == cursor.getString(cursor.getColumnIndex("target")) ? "1000" : cursor.getString(cursor.getColumnIndex("target"))));

            Log.w("--", "querySportData: ------------- 从数据库读到的运动数据：" + sportDataResult.toString());
        }
        db.close();
        return sportDataResult;
    }
}
